package com.edu.controller;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.edu.exception.FileException;
import com.edu.service.FileService;
import com.edu.vo.ResultVO;
import com.edu.vo.ServerConstant;

@RestController
@RequestMapping("/toolbar")
public class ToolbarController {

	@Autowired
	private FileService fileService;
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 文件相关的操作： 包括 创建文件(createDoc)、 创建文件夹(createDir)、
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/document", method = RequestMethod.GET)
	public ResultVO newDocument(@RequestParam(value = "name") String name) {
		name = name == null ? "" : name;
		try {
			fileService.createDocument(name);
		} catch (IOException | FileException e) {
			e.printStackTrace();
			return new ResultVO(ServerConstant.SERVER_INTERNAL_ERROR, e.getMessage());
		}
		return ResultVO.getSuccessResultVO();
	}

	@RequestMapping(value = "/directory", method = RequestMethod.GET)
	public ResultVO newDirectory(@RequestParam(value = "name") String name) {
		name = name == null ? "" : name;
		try {
			fileService.createDirectory(name);
		} catch (IOException | FileException e) {
			e.printStackTrace();
			return new ResultVO(ServerConstant.SERVER_INTERNAL_ERROR, e.getMessage());
		}
		return ResultVO.getSuccessResultVO();
	}

}
